import 'dart:async';
import 'package:amap_base/amap_base.dart';
import 'package:car_map/common/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';

class ShowMapScreen extends StatefulWidget {
  ShowMapScreen();

  factory ShowMapScreen.forDesignTime() => ShowMapScreen();

  @override
  _ShowMapScreenState createState() => _ShowMapScreenState();
}

class _ShowMapScreenState extends State<ShowMapScreen> {
  AMapController _controller;
  StreamSubscription _subscription;
  MyLocationStyle _myLocationStyle = MyLocationStyle();
  UiSettings _uiSettings = UiSettings();
  List<MarkerOptions> marList;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _updateMyLocationStyle(context,
        showMyLocation: false); //showMyLocation是否显示自己的位置
    getMarkerOptionsList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('监控'),
          centerTitle: true,
          actions: <Widget>[
            new PopupMenuButton(
                onSelected: (int value) {
                  setState(() {
                    setState(() {
                      _controller?.setMapType(value);
                    });
                  });
                },
                itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
                      new PopupMenuItem(
                          value: Constants.MAP_TYPE_NORMAL,
                          child: new Text("普通地图")),
                      new PopupMenuItem(
                          value: Constants.MAP_TYPE_SATELLITE,
                          child: new Text("卫星地图")),
                      new PopupMenuItem(
                          value: Constants.MAP_TYPE_NIGHT,
                          child: new Text("夜景地图")),
                      new PopupMenuItem(
                          value: Constants.MAP_TYPE_NAVI,
                          child: new Text("导航地图")),
                    ]),
          ],
        ),
        body: Column(
          children: <Widget>[
            Flexible(
              child: AMapView(
                onAMapViewCreated: (controller) {
                  _controller = controller;
                  //定位按钮
                  _controller.setUiSettings(
                      _uiSettings.copyWith(isMyLocationButtonEnabled: true));

                  //点击坐标显示坐标内信息
                  _controller.markerClickedEvent.listen((marker) {
                    Scaffold.of(context).showSnackBar(
                        SnackBar(content: Text(marker.toString())));
                  });
                  controller
                    ..addMarkers(
                      marList,
                    )
                    ..setZoomLevel(10);
                },
                amapOptions: AMapOptions(
                  //指定初始位置坐标
//                  compassEnabled: false,
//                  zoomControlsEnabled: true,
//                  logoPosition: LOGO_POSITION_BOTTOM_CENTER,
//                  camera: CameraPosition(
//                    target: LatLng(40.851827, 111.801637),
//                    zoom: 15,
//                  ),
                ),
              ),
            ),
          ],
        ));
  }

  //注销conller
  @override
  void dispose() {
    _controller?.dispose();
    _subscription?.cancel();
    super.dispose();
  }

  //权限请求
  void _updateMyLocationStyle(
    BuildContext context, {
    String myLocationIcon,
    double anchorU,
    double anchorV,
    Color radiusFillColor,
    Color strokeColor,
    double strokeWidth,
    int myLocationType,
    int interval,
    bool showMyLocation,
    bool showsAccuracyRing,
    bool showsHeadingIndicator,
    Color locationDotBgColor,
    Color locationDotFillColor,
    bool enablePulseAnnimation,
    String image,
  }) async {
    if (await Permissions().requestPermission()) {
      _myLocationStyle = _myLocationStyle.copyWith(
        myLocationIcon: myLocationIcon,
        anchorU: anchorU,
        anchorV: anchorV,
        radiusFillColor: radiusFillColor,
        strokeColor: strokeColor,
        strokeWidth: strokeWidth,
        myLocationType: myLocationType,
        interval: interval,
        showMyLocation: showMyLocation,
        showsAccuracyRing: showsAccuracyRing,
        showsHeadingIndicator: showsHeadingIndicator,
        locationDotBgColor: locationDotBgColor,
        locationDotFillColor: locationDotFillColor,
        enablePulseAnnimation: enablePulseAnnimation,
      );
      _controller?.setMyLocationStyle(_myLocationStyle);
    } else {
      Fluttertoast.showToast(
          msg: '权限不足',
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM);
    }
  }


  //制造车辆位置和信息假数据
  List<MarkerOptions> getMarkerOptionsList(){
    marList=new List();

    marList.add(new MarkerOptions(
      icon: 'assets/images/ic_run_gray_car.png',
      position: LatLng(34.2151780267, 108.8858306408),
      title: '王力宏',
      snippet: '奥迪A8L',
      rotateAngle:60,
      customCalloutView: '点击'
    ));

    marList.add(new MarkerOptions(
      icon: 'assets/images/ic_run_yellow_car.png',
      position: LatLng(34.2220047649, 108.8819146156),
      title: '范冰冰',
      snippet: '路虎揽胜',
      rotateAngle:40,
    ));

    marList.add(new MarkerOptions(
      icon: 'assets/images/ic_run_gray_car.png',
      position:  LatLng(34.2143573614, 108.8935875893),
      title: '刘德华',
      snippet: '兰博基尼-毒药',
      rotateAngle:180,
    ));

    marList.add(new MarkerOptions(
      icon: 'assets/images/ic_run_yellow_car.png',
      position:LatLng(34.2102938414, 108.8879656792),
      title: '周润发',
      snippet: 'JEEP指挥官',
      rotateAngle:360,
    ));

    marList.add(new MarkerOptions(
      icon: 'assets/images/ic_run_yellow_car.png',
      position:LatLng(34.2140911979, 108.8771295547),
      title: '孙俪',
      snippet: '奥迪TT',
      rotateAngle:10,
    ));

    marList.add(new MarkerOptions(
      icon: 'assets/images/ic_run_gray_car.png',
      position:LatLng(34.2133636803,108.8817858696),
      title: '张磊',
      snippet: '凯迪拉克',
      rotateAngle:30,
    ));

    marList.add(new MarkerOptions(
      icon: 'assets/images/ic_run_gray_car.png',
      position:LatLng(34.2115004966,108.8838672638),
      title: '张能能',
      snippet: '梅赛德斯奔驰',
      rotateAngle:160,
    ));

  }
}
